package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.Sight;
import com.yoyo.itravel.bean.TicketTypeDetail;
import com.yoyo.itravel.constants.MsgConstant;
import com.yoyo.itravel.dao.ITicketTypeDetailDao;
import com.yoyo.itravel.exceptions.TicketTypeDetailException;
import com.yoyo.itravel.service.ITicketTypeDetailService;
import com.yoyo.itravel.utils.DateUtils;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description 景点门票具体类型操作服务层接口实现类
 *
 * @author HZX
 * @date 2020/5/13 20:34
 */
@Service
public class TicketTypeDetailServiceImpl implements ITicketTypeDetailService {

    @Autowired
    private ITicketTypeDetailDao ticketTypeDetailDao;

    /**
     * @return java.util.List<cn.hut.travel.bean.TicketTypeDetail>
     * @Description 查找所有的景点门票具体类型
     */
    @Override
    public List<TicketTypeDetail> findAll() throws TicketTypeDetailException {
        try {
            return ticketTypeDetailDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new TicketTypeDetailException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @param detailTypeId
     * @return cn.hut.travel.bean.TicketTypeDetail
     * @Description 通过具体门票类型id获得具体门票类型对象
     */
    @Override
    public TicketTypeDetail findByTicketDetailTypeId(String detailTypeId) throws TicketTypeDetailException {
        try {
            // 通过具体门票类型id获得具体门票类型对象
            TicketTypeDetail ticketTypeDetail = ticketTypeDetailDao.selectByTicketDetailTypeId(detailTypeId);
            if (ticketTypeDetail == null) {
                return null;
            }
            // 获得开始预定的日期
            Date startOrderDate = ticketTypeDetail.getStartOrderDate();
            if (startOrderDate == null) {
                // 如果没有指定开始预定的日期，则默认为当前日期
                Date nowDate = new Date();
                String dateStr = DateUtils.parseDateToStr(nowDate, "yyyy-MM-dd");

                // 获得当前日期
                startOrderDate = DateUtils.strToDate(dateStr);
            } else {
                // 获得当前日期
                Date nowDate = DateUtils.strToDate(DateUtils.parseDateToStr(new Date(), "yyyy-MM-dd"));
                // 指定了开始预定的日期，判断该日期是是否大于或等于当前日期，如果小于，则替换为当前日期
                if (nowDate.getTime() - startOrderDate.getTime() > 0) {
                    startOrderDate = nowDate;
                }
            }

            // 设置预定截至日期
            Calendar c = Calendar.getInstance();
            c.setTime(startOrderDate);
            // 设置天数偏移量--可提前预定的天数
            c.add(Calendar.DATE, ticketTypeDetail.getEnOrderDays());
            // 构建预定截至日期对象
            Date stopOrderDate = c.getTime();

            // 重新设置预定开始和截止日期到ticketTypeDetail对象中
            ticketTypeDetail.setStartOrderDate(startOrderDate);
            ticketTypeDetail.setStopOrderDate(stopOrderDate);
            // 返回
            return ticketTypeDetail;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new TicketTypeDetailException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @param detailTypeId
     * @return User
     * @Description 通过门票具体类型id查找对应的景点对象
     */
    @Override
    public Sight findSight(String detailTypeId) throws TicketTypeDetailException {
        if (StringUtils.isEmpty(detailTypeId)) {
            throw new TicketTypeDetailException("门票具体类型id为空！");
        }
        try {
            return ticketTypeDetailDao.selectSightByDetailTypeId(detailTypeId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new TicketTypeDetailException(MsgConstant.INTERNAL_ERR);
        }
    }


}
